package com.family.pedigree.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.pedigree.domain.FamilyAlbum;
import com.family.pedigree.domain.FamilyAlbumRecord;
import com.family.pedigree.mapper.FamilyAlbumMapper;
import com.family.pedigree.mapper.FamilyAlbumRecordMapper;
import com.family.pedigree.service.IFamilyAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 相簿表 服务实现类
 * </p>
 *
 * @author forever
 * @since 2024-11-25
 */
@Service
@RequiredArgsConstructor
public class FamilyAlbumServiceImpl extends ServiceImpl<FamilyAlbumMapper, FamilyAlbum> implements IFamilyAlbumService {

    private final FamilyAlbumRecordMapper recordMapper;

    @Override
    public void add(FamilyAlbum album) {
        save(album.setCreateBy("admin").setCreateTime(LocalDateTime.now()));
    }

    @Override
    public void edit(FamilyAlbum album) {

        updateById(album);

    }

    @Override
    public void delById(Long albumId) {
        Long count = recordMapper.selectCount(Wrappers.<FamilyAlbumRecord>lambdaQuery().eq(FamilyAlbumRecord::getAlbumId, albumId));
        if (count > 0) throw new RuntimeException("相簿下面还有照片不能删除");

        removeById(albumId);
        //todo:还要删除照片minio
    }

    @Override
    public FamilyAlbum getInfo(Long albumId) {
        FamilyAlbum album = getById(albumId);
        List<FamilyAlbumRecord> familyAlbumRecords = recordMapper.selectList(Wrappers.<FamilyAlbumRecord>lambdaQuery().eq(FamilyAlbumRecord::getAlbumId, albumId));
        return album.setRecords(familyAlbumRecords);
    }
}
